/* eslint-disable vue/valid-v-for */
<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="课程名称" prop="courseId">
        <el-select v-model="queryParams.courseId" placeholder="请选择课程" clearable size="small">
          <el-option
            v-for="item in gradeList"
            :key="item.courseId"
            :value="item.courseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="gradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="成绩ID" align="center" prop="gradeId" />
      <el-table-column label="学生名称" align="center" prop="studentId" />
      <el-table-column label="课程名称" align="center" prop="courseId" />
      <el-table-column label="成绩" align="center" prop="resultGrade" />
      <el-table-column label="补考成绩" align="center" prop="lastResultGrade" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listGradeByLoginId } from '@/api/zua/grade'

export default {
  name: 'Grade',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成绩表格数据
      gradeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        courseId: null,
        resultGrade: null,
        lastResultGrade: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        studentId: [
          { required: true, message: '学生id不能为空', trigger: 'change' }
        ],
        courseId: [
          { required: true, message: '课程id不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询成绩列表 */
    getList() {
      this.loading = true
      listGradeByLoginId(this.queryParams).then(response => {
        this.gradeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        gradeId: null,
        studentId: null,
        courseId: null,
        resultGrade: null,
        lastResultGrade: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gradeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
  }
}
</script>
